package com.whfc.mach.service.impl;

import com.whfc.common.exception.BizException;
import com.whfc.common.geometry.GeometryUtil;
import com.whfc.common.geometry.Point;
import com.whfc.common.geometry.Polygon;
import com.whfc.mach.dao.AppMachConfigMapper;
import com.whfc.mach.dao.AppMachStrategyMapper;
import com.whfc.mach.dto.MachValidFenceDTO;
import com.whfc.mach.dto.strategy.MachStrategyDTO;
import com.whfc.mach.entity.AppMachConfig;
import com.whfc.mach.entity.AppMachStrategy;
import com.whfc.mach.enums.MachTypeFence;
import com.whfc.mach.redis.MachPolygonWKTRedisDao;
import com.whfc.mach.service.AppMachConfigService;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author sun_guodong
 * @description
 * @date 2021-01-25
 */
@DubboService(interfaceClass = AppMachConfigService.class, version = "1.0.0")
public class AppMachConfigServiceImpl implements AppMachConfigService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MachPolygonWKTRedisDao machPolygonWktRedisDao;

    @Autowired
    private AppMachStrategyMapper appMachStrategyMapper;

    @Autowired
    private AppMachConfigMapper appMachConfigMapper;

    @Override
    public void refreshPolygonWKT(Integer deptId) {
        logger.info("刷新设备polygonWKT缓存,deptId:{}", deptId);
        machPolygonWktRedisDao.delPolygonWKT(deptId);
    }

    @Override
    public List<MachStrategyDTO> getStrategyConfig(Integer deptId) throws BizException {
        return appMachStrategyMapper.selectStrategyByDeptId(deptId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void strategyConfigure(Integer deptId, List<MachStrategyDTO> list) throws BizException {
        //删除旧数据
        appMachStrategyMapper.logicDelByDeptId(deptId);
        //保存新的计算策略
        for (MachStrategyDTO machStrategyDTO : list) {
            AppMachStrategy strategy = new AppMachStrategy();
            strategy.setDeptId(deptId);
            strategy.setOilStrategy(machStrategyDTO.getOilStrategy());
            strategy.setWorkStrategy(machStrategyDTO.getWorkStrategy());
            appMachStrategyMapper.insertSelective(strategy);
        }
    }

    @Override
    public List<MachValidFenceDTO> getMachValidFenceList(Integer deptId) {
        List<MachValidFenceDTO> list = new ArrayList<>();
        List<String> codeList = Arrays.stream(MachTypeFence.values()).map(MachTypeFence::getCode).collect(Collectors.toList());
        List<AppMachConfig> machConfigList = appMachConfigMapper.selectByDeptIdAndCodeList(deptId, codeList);
        for (AppMachConfig appMachConfig : machConfigList) {
            MachValidFenceDTO machValidFenceDTO = new MachValidFenceDTO();
            machValidFenceDTO.setCode(appMachConfig.getCode());
            String value = appMachConfig.getValue();
            if (value.startsWith("POLYGON")) {
                Polygon polygon = GeometryUtil.decodePolygon(value);
                machValidFenceDTO.setPolygonPointList(polygon.getPointList());
            } else if (value.startsWith("MULTIPOLYGON")) {
                List<List<Point>> multiPolygon = GeometryUtil.decodeMultiPolygon(value);
                machValidFenceDTO.setMultiPolygonPointList(multiPolygon);
            } else {
                continue;
            }
            list.add(machValidFenceDTO);
        }
        return list;
    }

    @Override
    public void setMachValidFence(Integer deptId, List<MachValidFenceDTO> list) {
        //删除旧数据
        List<String> codeList = Arrays.stream(MachTypeFence.values()).map(MachTypeFence::getCode).collect(Collectors.toList());
        appMachConfigMapper.delByDeptIdAndCodeList(deptId, codeList);
        //保存新数据
        for (MachValidFenceDTO machValidFenceDTO : list) {
            AppMachConfig machConfig = new AppMachConfig();
            machConfig.setDeptId(deptId);
            machConfig.setCode(machValidFenceDTO.getCode());
            List<Point> polygonPointList = machValidFenceDTO.getPolygonPointList();
            List<List<Point>> multiPolygonPointList = machValidFenceDTO.getMultiPolygonPointList();
            String polygonKwt;
            if (polygonPointList != null && !polygonPointList.isEmpty()) {
                Polygon polygon = new Polygon();
                polygon.setPointList(polygonPointList);
                polygonKwt = GeometryUtil.encodePolygon(polygon);
                machConfig.setValue(polygonKwt);
            } else if (multiPolygonPointList != null && !multiPolygonPointList.isEmpty()) {
                String multiPolygon = GeometryUtil.encodeMultiPolygon(multiPolygonPointList);
                machConfig.setValue(multiPolygon);
            } else {
                //空的数据
                continue;
            }
            appMachConfigMapper.insertSelective(machConfig);
        }
        //清除缓存
        machPolygonWktRedisDao.delPolygonWKT(deptId);
    }
}
